#include <stdio.h>  // for printf
#include <string.h> // for memory operation
#include <stdlib.h>
#include <unistd.h> // for sleep
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "lwip/netifapi.h"
#include "lwip/api_shell.h"
#include "lwip/sockets.h"
#include "iot_i2c.h"
#include "iot_i2c_ex.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "temp_humi.h"

#define I2C_SDA_GPIO_IDX 13
#define I2C_SCL_GPIO_IDX 14
#define WIFI_IOT_I2C_IDX_0 0
#define SHT30_ADDR 0x44

float temp;
float humi;

const unsigned char yintian[288] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                    0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80,
                                    0x00, 0x00, 0x00, 0x02, 0x00, 0x80, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x30, 0x00, 0x00, 0x01, 0x80, 0x00, 0x60, 0x00,
                                    0x00, 0x00, 0x87, 0xf0, 0xc0, 0x00, 0x00, 0x00, 0x0f, 0xfc, 0x00, 0x00, 0x00, 0x3c, 0x07, 0xfe, 0x00, 0x00, 0x01, 0xff,
                                    0x83, 0xff, 0x00, 0x00, 0x07, 0xff, 0xe1, 0xff, 0x00, 0x00, 0x0f, 0xff, 0xf0, 0xff, 0x80, 0x00, 0x1f, 0xff, 0xf8, 0xff,
                                    0x80, 0x00, 0x1f, 0xff, 0xfc, 0xff, 0x80, 0x00, 0x3f, 0xff, 0xfc, 0xff, 0x8f, 0x00, 0x3f, 0xff, 0xfc, 0x43, 0x80, 0x00,
                                    0x3f, 0xff, 0xfe, 0x00, 0x80, 0x00, 0x3f, 0xff, 0xfe, 0x60, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xfc, 0x00, 0x00, 0x3f, 0xff,
                                    0xff, 0xfe, 0x00, 0x00, 0x3f, 0xff, 0xff, 0xff, 0x00, 0x03, 0xff, 0xff, 0xff, 0xff, 0x80, 0x0f, 0xff, 0xff, 0xff, 0xff,
                                    0xc0, 0x1f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f,
                                    0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff,
                                    0xff, 0xff, 0xe0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xc0, 0x3f, 0xff, 0xff, 0xff, 0xff,
                                    0x80, 0x3f, 0xff, 0xff, 0xff, 0xff, 0x00, 0x1f, 0xff, 0xff, 0xff, 0xfe, 0x00, 0x07, 0xff, 0xff, 0xff, 0xfc, 0x00, 0x00,
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

static int temp_humi_task()
{

    //初始化sht30
    InitSHT30();

    OLED_Init();			   //初始化OLED  
    OLED_Clear(0);             //清屏（全黑）
    while (1)
    {
        SHT3X_ReadMeasurementBuffer((float *)&temp, (float *)&humi);
        printf("Temperature : %.1f\nHumidity : %.1f\n", temp, humi);
        sleep(2);
        int year = 2022;
        int month = 7;
        int day = 7;
        GUI_DrawRectangle(8, 0, 125, 61, 1); //大框

        GUI_DrawRectangle(8, 0, 64, 16, 1); //日期框

        GUI_ShowString(9, 3, "2022-7-8", 8, 1);

        GUI_DrawRectangle(11, 20, 60, 50, 1); //湿温度框
                                              // printString(13,18,16,"temp:");
        GUI_ShowString(13, 23, "temp:", 8, 1);
        int t = (int)temp;

        GUI_ShowNum(40, 23, t, 2, 8, 1); //温度

        GUI_DrawLine(11, 35, 60, 35, 1); //上下分割线

        GUI_ShowString(13, 40, "humi:", 8, 1);
        GUI_DrawLine(16, 0, 30, 0, 1);
        int h = (int)humi;
        GUI_ShowNum(40, 40, h, 2, 8, 1); //湿度

        GUI_DrawLine(64, 0, 64, 61, 1); //左右分割线

        GUI_DrawBMP(70, 2, 48, 48, yintian, 1); //右侧图标
        
        GUI_ShowString(75,50,"Cloudy",8,1);//阴天
        sleep(2);
    }
}

int temp_humi_init()
{
    osThreadAttr_t attr;
    attr.name = "temp_humi_task"; //"task_main_entry"
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 4096;
    attr.priority = 20;
    if (osThreadNew((osThreadFunc_t)temp_humi_task, NULL, &attr) == NULL)
    {
        printf("Failed to create tcp_server_task!\n");
    }
}
APP_FEATURE_INIT(temp_humi_init);
